package com.mycommune.servlet;

import com.mycommune.servlet.ChatSessionFactory.ChatSession;

public class ChatHandler {

	public static String handleChat(ChatSession chatSession, String body) {
		StringBuffer reply = new StringBuffer();
		if (chatSession.getObject() == null){
			chatSession.loadObject(body);
			if (chatSession.getObject() == null) {
				reply.append("Enter the object name?\n");
			} else {
				reply.append("Class " + chatSession.getObject()
						+ " loaded successfully");
				reply.append("Enter the option : 1. For SELECT");
			}
			return reply.toString();
		}
		else if(chatSession.getOption() == ChatSession.BLANK){
			try{
				int option = Integer.parseInt(body);
				chatSession.setOption(option);
				reply.append("Option set for SELECT");
			}
			catch(NumberFormatException e){
				reply.append("Enter the option : 1. For SELECT");
				return reply.toString();
			}
		}
		
		switch (chatSession.getOption()) {
			case 1:
				{
					reply.append("SELECT e FROM "+chatSession.getObject()+" e");
					reply.append(chatSession.loadAllEntities());
					break;
				}
			default:
				break;
		}
		
		
		return reply.toString();
	}

}
